package com.inspur.edp.common.expr.visitor.array;

import com.inspur.edp.common.expr.ast.ASTContext;
import com.inspur.edp.common.expr.ast.CompileUnitParser;
import com.inspur.edp.common.expr.ast.CompileUnitVisitor;
import com.inspur.edp.common.expr.spi.ArrayAccessVisitor;
import com.inspur.edp.common.expr.visitor.JavaVisitor;
import com.inspur.edp.common.expr.visitor.VisitResult;


/**
 * @author lizhaorui
 * @description TODO
 * @date 2024/9/12 11:02
 */
public class DefaultArrayAccessVisitor implements ArrayAccessVisitor {

    public static final String KIND = "default";

    @Override
    public String getKind() {
        return KIND;
    }

    @Override
    public VisitResult visitGetter(CompileUnitVisitor visitor,
                                   ASTContext context,
                                   CompileUnitParser.ExpressContext objectExpr,
                                   CompileUnitParser.ExpressContext indexExpress) {

        JavaVisitor javaVisitor = (JavaVisitor) visitor;
        String code = javaVisitor.visitExpress(objectExpr) +
                "[" + javaVisitor.visitExpress(indexExpress) + "]";
        return new VisitResult(code);

    }

    @Override
    public VisitResult visitSetter(CompileUnitVisitor visitor,
                                   ASTContext context,
                                   CompileUnitParser.ExpressContext objectExpr,
                                   CompileUnitParser.ExpressContext indexExpress,
                                   CompileUnitParser.ExpressContext valueExpress) {

        JavaVisitor javaVisitor = (JavaVisitor) visitor;
        String code = javaVisitor.visitExpress(objectExpr) +
                "[" + javaVisitor.visitExpress(indexExpress) + "]" +
                " = " +
                javaVisitor.visitExpress(valueExpress);
        return new VisitResult(code);

    }
}
